
package view;

import java.awt.Component;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author FelipeJ
 */
public class Utilities {
    
    
    
    //replaces a substring in a string 
    public static String replace(String str, String pattern, String replace) {
        int s = 0;
        int e = 0;
        StringBuffer result = new StringBuffer();

        while ((e = str.indexOf(pattern, s)) >= 0) {
            result.append(str.substring(s, e));
            result.append(replace);
            s = e + pattern.length();
        }
        result.append(str.substring(s));
        return result.toString();
}
    
    ////////////////////
    //    DIALOGS     //
    ////////////////////
    
    
    public static void messageDialogInfo(String message, Component cmp){
         JOptionPane.showMessageDialog(cmp, message, "Information",JOptionPane.INFORMATION_MESSAGE);
    }
    

       
    public static void messageDialogWarning (String message, Component cmp, URL urlIcon){
        JOptionPane.showMessageDialog(cmp, message, "Warning",JOptionPane.WARNING_MESSAGE , new javax.swing.ImageIcon(urlIcon));
    }
    
    
    public static void messageDialogOK (String message, Component cmp,URL urlIcon){
        JOptionPane.showMessageDialog(cmp, message, "OK", JOptionPane.INFORMATION_MESSAGE, new javax.swing.ImageIcon(urlIcon));
    }
    
    //return true if the user wants to continue
    public static boolean messageOptionDialog(String messageQuestion,Component cmp,URL urlIcon){
        
        Object[] options = {"Yes, continue","No, cancel!"};
        
        int i =JOptionPane.showOptionDialog(cmp,messageQuestion,"Question",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,new javax.swing.ImageIcon(urlIcon),options,options[0]);
        
        if (i==0){
            return true;
        }
        else{
            return false;
        }

    }
    
    
    ///////////////////////////////
    //    VALIDATION METHODS     //
    ///////////////////////////////
    
    
    //check for the presence of especial charaters that user CANNOT use
    //returns the characters found, if return "" empty, then its OK
    public static String checkEspCharacters(ArrayList<String> fields) {

        String[] characters = new String[]{"@", "%", "~", "&", "\r"};
        String result = "";

        for (String character : characters) {
            for (String field : fields) {
                if (field.indexOf(character) >= 0) { //index of -1 means that the character is not present in the field
                    result = result + character + "  ";
                    break;  //stop searching for that character it it was found one time
                }
            }
        }
        return result;
    }
    
    //overloaded version for checking only one string
    public static String checkEspCharacters(String field){
        
        String[] characters = new String[]{"@", "%", "~", "&", "\r"};
        String result = "";

        for (String character : characters) {
                if (field.indexOf(character) >= 0) { //index of -1 means that the character is not present in the field
                    result = result + character + "  ";
                }
        }
        return result;
    }
    
    
    
    
    public static boolean isInteger(String string) {
        try {
            Integer.valueOf(string);
            return true;
        } 
        catch (NumberFormatException e) {
            return false;
        }
    }
    
    
    //check if it is a positive integer (includes zero)
    
    //returns false if string is not an integer or not positive
    public static boolean isPositiveInteger(String string) {
        
        try {
            if (Integer.valueOf(string)>=0){
            return true;
            }
            else{
                return false;
            }
        } 
        catch (NumberFormatException e) {
            return false;
        }
    }
    
    
    
    public static boolean isFloat(String string) {

        if (string == null) {
            return false;
        } else {
            try {
                Float.valueOf(string);
                return true;
            } catch (NumberFormatException e) {
                return false;
            }
        }


    }
    
    public static boolean isPositiveFloat(String string) {
        
        try {
            if (Float.valueOf(string)>=0){
            return true;
            }
            else{
                return false;
            }
        } 
        catch (NumberFormatException e) {
            return false;
        }
    }
    
}
